import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
	namespaced: true,//在这里控制  
	state: {
		// 全局的状态
		// 这是变量
		musicUrl: '',
		// 当前用户
		currentUser: null,
		// 判断当前用户是否已经登录
		isLogin: false,
		//这里先通过session获取用户信息。如果sessionStorage.getItem('token')  获取不到。就为''空字符串。相当于没登录。登录成功后session里就有内容了。懂了
		// 是保存用户登录后持有的toke
		token: sessionStorage.getItem('token') || '',
		//用户头像
		avatarUrl: sessionStorage.getItem('avatarUrl') || '',

		//用户昵称
		nickname: sessionStorage.getItem('nickname') || '',

	},
	// 更新数据
	mutations: {

		setmMusicUrl: (state, musicUrl) => {
			console.log(musicUrl)
			state.musicUrl = musicUrl
		},

		userStatus: (state, user) => {
			if (user) {
				state.currentUser = user
				state.isLogin = true
			} else if (user == null) {
				sessionStorage.setItem('userName', null)
				sessionStorage.setItem('userToken', '')
				state.currentUser = null
				state.isLogin = false
				state.token = ''
			}
		},
		set_user_state: (state, obj) => {
			//这里修改vuex state里面的内容
			state.token = obj.token
			state.avatarUrl = obj.avatarUrl
			state.nickname = obj.nickname
//这里存到session里面
			sessionStorage.setItem('token', obj.token)
			sessionStorage.setItem('avatarUrl', obj.avatarUrl)
			sessionStorage.setItem('nickname', obj.nickname)
		},
	},
	// 方法
	actions: {
		SETMUSICURL({ commit }, musicUrl) {
			console.log(musicUrl)
			commit('setmMusicUrl', musicUrl)
		},
		SETUser({ commit }, user) {
			commit('userStatus', user)
		},
		//这里调用mutation里面的方法。
		SETUSERSTATE({ commit }, obj) {
			commit('set_user_state', obj)
		}
	},
	modules: {


	}
})
